package com.ln.cmsln.controller.zlxl;


import com.alibaba.fastjson.JSON;
import com.ln.cmsln.mapper.HeadimageMapper;
import com.ln.cmsln.pojo.entity.Headimage;
import com.ln.cmsln.service.FeilService;
import com.ln.cmsln.service.HeadimageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wuwenzhe
 * @since 2022-03-31
 */
@CrossOrigin//跨域
@RestController
@RequestMapping("/headimage")
public class HeadimageController {
    @Resource
    private HeadimageMapper headimageMapper;
    @Resource
    private FeilService fileService;

    @ApiOperation("全查")
    @RequestMapping("getAll")
    public String getAll(int page, int limit) {
//        PageHelper.startPage(page,limit);
        List<Headimage> headimages = headimageMapper.selectList(null);
        int count = headimages.size();

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", headimages);

        String json = JSON.toJSONString(map);
        System.out.println("json = " + json);
        return json;
    }

    @RequestMapping("delete")
    public String delete(@Param("id") Integer id,String url) {
        fileService.removeFile(url);
        System.out.println("id = " + id);
        int res = headimageMapper.deleteById(id);
        String json = JSON.toJSONString(res);
        return json;
    }

    @ApiOperation("文件上传")
    @RequestMapping("/upload")
    public String upload(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file") MultipartFile file
    ) throws IOException {
        String module = "ln-oa-file" ;
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String upload = fileService.upload(inputStream, module, originalFilename);

        int code = 0 ;
        Map<String,Object> map = new HashMap<>();
        if (upload.length()>0){
            code = 1 ;
        }else {
            code = -1 ;
        }
        map.put("code",code);
        map.put("msg",upload);
        String json = JSON.toJSONString(map) ;
        System.out.println("文件上传:json = " + json);
        return json ;
    }

    @RequestMapping("add")
    public String add(Headimage headimage) {
        int res = headimageMapper.insert(headimage);
        String json = JSON.toJSONString(res);
        return json ;
    }

}

